/*
2017-05-04
*/
var ROOT='http://api.hongyanche.com';

CONSOLE = {
	urls:{
		unit_label		: ROOT+"/unit/label",

		unit_list		: ROOT+"/network/getlist" ,
		unit_create		: ROOT+"/network/create" ,
		unit_remove		: ROOT+"/network/remove" ,
		unit_modify		: ROOT+"/network/modify" ,
		unit_highlight	: ROOT+"/network/highlight" ,

		unit_release   		: ROOT+"/network/release",
		unit_invite			: ROOT+"/network/invite",
		unit_invite_cancel	: ROOT+"/network/invite_cancel",
		unit_publish		: ROOT+"/network/publish",
		unit_apply_dispose	: ROOT+"/network/apply_dispose",

		umeditor_img_upload : ROOT+"/upload/unit",
	},
	init:function(){
		this.editor.init();
	},
	editor:{
		init:function(){
			this.unit.init();

			this.umeditor.init();
		},
		unit:{
			init:function(){
				var self = this;
				POST.init(document,{
					confirm:true,
					input:function(){
						var labels = $('#editor-unit-list-labels-input').val();
						return {labels:labels};
					},
					text:'添加',
					success:function(){
						LIST.reload('#editor-unit-list')
					},
					url:CONSOLE.urls.unit_create
				},"#editor-unit-create")
				//remove
				POST.init(document,{
					stop:function(){
						return LIST.selected('#editor-unit-list').length <=0;
					},
					input:function(){
						var unit_id = LIST.selected('#editor-unit-list');
						unit_id = unit_id.join(',');
						return {unit_id:unit_id};
					},
					text:'删除',
					confirm:function(){
						return {target:LIST.selected('#editor-unit-list').length+"个产品"}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
					},
					url:CONSOLE.urls.unit_remove
				},"#editor-unit-remove")
				//copy
				POST.init(document,{
					stop:function(){
						return LIST.selected('#editor-unit-list').length <=0;
					},
					input:function(){
						var unit_id = LIST.selected('#editor-unit-list');
						unit_id = unit_id.join(',');
						return {unit_id:unit_id};
					},
					text:'复制',
					confirm:function(){
						return {target:LIST.selected('#editor-unit-list').length+"个产品"}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
					},
					url:CONSOLE.urls.unit_copy
				},"#editor-unit-copy")
				//modify
				POST.init(document,{
					method:'change',
					url:CONSOLE.urls.unit_modify,
					style:'input',
					stop:function(){
						var unit_id = $("#detail-unit_id").html();
						if(!unit_id) return true;
					},
					input:function(e){
						var unit_id = $("#detail-unit_id").html();
						var key = $(e).attr('name');
						var content = $(e).val();
						return {unit_id:unit_id,key:key,content:content}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
					}
				},'.editor-unit-modify')

				//highlight
				POST.init(document,{
					method:'change',
					url:CONSOLE.urls.unit_highlight,
					style:'input',
					text:"置顶",
					stop:function(){
						var unit_id = $("#detail-unit_id").html();
						if(!unit_id) return true;
					},
					input:function(e){
						var unit_id = $("#detail-unit_id").html();
						var highlight = parseInt($(e).prop('checked')*1) || 0;
						return {unit_id:unit_id,highlight:highlight}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
					}
				},'#editor-unit-highlight')

				//release ／ close
				POST.init(document,{
					url:CONSOLE.urls.unit_release,
					confirm:true,
					text:"重新开放",
					stop:function(){
						var unit_id = $("#detail-unit_id").html();
						if(!unit_id) return true;
					},
					input:function(e){
						var unit_id = $("#detail-unit_id").html();
						return {unit_id:unit_id,release:1}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
						VALUE.write('#editor-panel-unit-detail closed','detail',0);
						VALUE.write('#editor-panel-unit-detail published','detail',1);
					}
				},'#unit-release')
				POST.init(document,{
					url:CONSOLE.urls.unit_release,
					confirm:true,
					text:"关闭",
					stop:function(){
						var unit_id = $("#detail-unit_id").html();
						if(!unit_id) return true;
					},
					input:function(e){
						var unit_id = $("#detail-unit_id").html();
						return {unit_id:unit_id,release:0}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
						VALUE.write('#editor-panel-unit-detail published','detail' ,0);
						VALUE.write('#editor-panel-unit-detail closed'	  ,'detail' ,1);
					}
				},'#unit-close')
				//publish
				POST.init(document,{
					url:CONSOLE.urls.unit_publish,
					confirm:true,
					text:"发布",
					stop:function(){
						var unit_id = $("#detail-unit_id").html();
						if(!unit_id) return true;
					},
					input:function(e){
						var unit_id = $("#detail-unit_id").html();
						return {unit_id:unit_id}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
						VALUE.write('#editor-panel-unit-detail published','detail',1);
						VALUE.write('#editor-panel-unit-detail editing','detail',0);
					}
				},'#unit-publish')
				//publish
				POST.init(document,{
					url:CONSOLE.urls.unit_apply_dispose,
					confirm:true,
					text:"通过活动申请",
					stop:function(){
						var apply_id = $("#option-unit-detail-apply_id").html();
						if(!apply_id) return true;
					},
					input:function(e){
						var apply_id = $("#option-unit-detail-apply_id").html();
						return {apply_id:apply_id,approve:1}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
						VALUE.write('#editor-panel-unit-detail published','detail',1);
						VALUE.write('#editor-panel-unit-detail applying','detail',0);
					}
				},'#unit-apply-approve')
				//rejected
				POST.init(document,{
					url:CONSOLE.urls.unit_apply_dispose,
					confirm:true,
					text:"拒绝活动申请",
					stop:function(){
						var apply_id = $("#option-unit-detail-apply_id").html();
						if(!apply_id) return true;
					},
					input:function(e){
						var apply_id = $("#option-unit-detail-apply_id").html();
						var reason = $("#unit-apply-reject-reason").val();
						return {apply_id:apply_id,approve:0,reason:reason}
					},
					success:function(){
						LIST.reload('#editor-unit-list')
						VALUE.write('#editor-panel-unit-detail rejected','detail',1);
						VALUE.write('#editor-panel-unit-detail applying','detail',0);
					}
				},'#unit-apply-reject')
			},
			open:function(e){
				$('#editor-panel-label-config').hide();
				$('#editor-panel-unit-detail').fadeIn();
				DETAIL.put($('#editor-panel-unit-detail'),$(e).find('.bc-list-detail-box'));
			}
		},
		umeditor:{
			init:function(){
				var um = null;
				$(document).on('bc-page-success', '.page.editor_article', function(event) {
					var unit_id = $("#detail-unit_id").html();
					var name = $("#editor-unit-name").val();
					if(!unit_id){
						if(PAGE.stack.length){history.back();}else{location.href='#editor'}
					}
					$("#editor-umeditor name").html(name)
					$("#editor-umeditor unit_id").html(unit_id)
					if(um) um.destroy();
					um = UM.getEditor('myEditor',{
						imageUrl:CONSOLE.urls.umeditor_img_upload+"?unit_id="+unit_id,
						imagePath:""    ,   
					});
					var text = $("#unit-editor-article").html();
					um.setContent(text,false);
				});
				POST.init(document,{
					text:"保存文案",
					confirm:true,
					url:CONSOLE.urls.unit_modify,
					stop:function(){
						var unit_id = $("#detail-unit_id").html();
						if(!unit_id) return true;
					},
					input:function(e){
						var unit_id = $("#detail-unit_id").html();
						var content = um.getContent();
						return {unit_id:unit_id,key:'article',content:content}
					},
					success:function(){
						LIST.reload('#editor-unit-list');
						var content = um.getContent();
						$("#unit-editor-article").html(content);
					}
				},'#unit-article-modify')
			}
		}
	}
}
CONSOLE.init();